package com.example.hibernate.a.helloWorld;

import org.hibernate.Session;
import org.junit.Test;

import com.example.hibernate.utils.HibernateUtils;

public class TestMessage {
	
	public static void main(String[] args) {
	}
	
	@Test
	public void testSave(){
		Message message = new Message();
//		message.setText("nine message.");
		
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		
		Long id = (Long) session.save(message);
		
		session.getTransaction().commit();
		
		//----------------------
		
		Message message2 = new Message();
		message2.setText("ten message.");
		message2.setNextMessage(message);
		
		session = HibernateUtils.openSession();
		session.beginTransaction();
		Long id2 = (Long) session.save(message2);
		
		session.getTransaction().commit();
		System.out.println(message2.getId());
		
	}
	
	
	@Test
	public void testGet(){
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		
		Message message = (Message) session.get(Message.class, 2L);
		
		session.getTransaction().commit();
		
		System.out.print(message);
		if(message.getNextMessage() != null){
			System.out.println("  nextID: " + message.getNextMessage().getId());
		}
	}
	
	@Test
	public void testDelete(){
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		
		Message message = (Message) session.get(Message.class, 5L);
		if(message != null){
			session.delete(message);
		}
		
		session.getTransaction().commit();
	}
	
	@Test
	public void testUpdate(){
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		
		Message message = (Message) session.get(Message.class, 17L);
		if(message != null){
			message.setText("another22");
			session.update(message);
		}
		
		session.getTransaction().commit();
	}
	
}
